﻿using System;
using System.Web;
using System.Web.UI.WebControls;
using Medianamik.Core;
using Medianamik.Core.Sugar;
using Medianamik.UI.Web.UserControls;

namespace Medianamik.UI.ContentManagement
{
	public partial class SearchResults : BaseSearchResultPage 
	{
		 
		protected void Page_Load(object sender, EventArgs e)
		{
            if (!Page.IsPostBack)
            {
                int currentPageIndex;
                int.TryParse(Request["PageIndex"], out currentPageIndex);
                GridNodes.CurrentPageIndex = currentPageIndex;
                GridNodes.DataBind();
                var sb = MyMasterPage.FindControl("SearchBox") as SearchBox;
                if (sb != null)
                {
                    sb.SearchText = Query;
                }
            }
            
		}

        protected void GridNodes_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            Result.PageSize = 20;
			GridNodes.DataSource = Result.GetNodes(GridNodes.CurrentPageIndex);
            GridNodes.VirtualItemCount = Result.HitCount;
            GridNodes.PageSize = Result.PageSize;
        }
                
		protected override Guid BaseNodeTypeId
		{
            get { return MedianamikTypes.BaseContentType; }
		}

        protected override Guid BaseParentNodeId
        {
            get { return MedianamikModules.ContentManagement; }
        }

        protected void LockLink_RequestFailed(object sender, CommandEventArgs e)
        {
            GridNodes.Rebind();
            LockInfoTooltip.ObjectId = new Guid((string)e.CommandArgument);
            LockInfoTooltip.DataBind();
            LockInfoTooltip.Show();

        }

        protected void LockInfoTooltip_LockDeleted(object sender, EventArgs e)
        {
            GridNodes.Rebind();
        }

        protected string ReturnUrl
        {
            get
            {
                var qs = HttpUtility.ParseQueryString(Context.Request.Url.Query);
                qs["PageIndex"] =  GridNodes.CurrentPageIndex.ToString();
                return (Context.Request.Path + "?" + qs).EncodeBase64();

            }
        }

        protected override void OnCultureChanged(CultureChangedEventArgs e)
        {
            base.OnCultureChanged(e);
            GridNodes.Rebind();
        }
	}
}
